﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace MVCPets.Extensions
{
  public static class ExceptionExtensions
  {
    /// <summary>
    /// Sends the exception details as an email
    /// </summary>
    /// <param name="e"></param>
    /// <param name="toAddress"></param>
    public static void SendEmail(this Exception e, string toAddress)
    {
      //SendEmail(e, "mail.w-win.com", "ExceptionExtension@w-win.com", "Application Exception", toAddress);
    }

    /// <summary>
    /// Sends the exception details as an email
    /// </summary>
    /// <param name="e"></param>
    /// <param name="subject"></param>
    /// <param name="toAddress"></param>
    public static void SendEmail(this Exception e, string subject, string toAddress)
    {
      //SendEmail(e, "mail.w-win.com", "ExceptionExtension@w-win.com", subject, toAddress);
    }

    /// <summary>
    /// Sends the exception details as an email
    /// </summary>
    /// <param name="e"></param>
    /// <param name="fromAddress"></param>
    /// <param name="subject"></param>
    /// <param name="toAddress"></param>
    public static void SendEmail(this Exception e, string fromAddress, string subject, string toAddress)
    {
      //SendEmail(e, "mail.w-win.com", fromAddress, subject, toAddress);
    }

    /// <summary>
    /// Sends the exception details as an email
    /// </summary>
    /// <param name="e">The Exception</param>
    /// <param name="server">mail server</param>
    /// <param name="fromAddress">Address to send from</param>
    /// <param name="subject">The email subject</param>
    /// <param name="toAddress">Address of recipient</param>
    public static void SendEmail(this Exception e, string server, string fromAddress, string subject, string toAddress)
    {
      SmtpClient mailClient = new SmtpClient(server);
      MailMessage msg = new MailMessage();

      msg.From = new MailAddress(fromAddress);

      foreach (var m in toAddress.Split(','))
      {
        msg.To.Add(new MailAddress(m));
      }

      msg.Subject = subject;
      msg.IsBodyHtml = false;
      msg.Body = e.GetExceptionDetailString(true);
      mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

      mailClient.Send(msg);
    }

    /// <summary>
    /// Returns the Exception details as a string
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    public static string GetExceptionDetailString(this Exception e)
    {
      return GetExceptionDetailString(e, false);
    }

    /// <summary>
    /// Returns the Exception details as a string
    /// </summary>
    /// <param name="e"></param>
    /// <param name="serverDetails">True to include additional server details</param>
    /// <returns></returns>
    public static string GetExceptionDetailString(this Exception e, bool serverDetails)
    {
      StringBuilder sb = new StringBuilder();

      sb.Append(ParseException(e)).AppendLine().AppendLine();
      if (serverDetails) sb.AppendServerDetails();

      return sb.ToString();
    }

    #region Private Methods
    private static string ParseException(Exception e)
    {
      StringBuilder sb = new StringBuilder();

      sb.AppendFormat("Error Message: {0}<br />", e.Message);
      sb.AppendFormat("Stack Trace: {0}<br />", e.StackTrace);
      sb.AppendFormat("Source: {0}<br />", e.Source);
      sb.AppendFormat("Type: {0}<br />", e.GetType().ToString());

      if (e.InnerException != null)
      {
        sb.AppendLine().AppendLine();
        sb.Append(ParseException(e.InnerException));
      }
      return sb.ToString();
    }
    #endregion
  }
}
